package com.team.game.framework.Abstract;


import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;

import com.team.game.framework.Game;
import com.team.game.framework.impl.SurfaceThread;

public abstract class Tower {
    protected Game game;

    protected Bitmap boom, tower, attack;

    protected RectF rectBoom, rectAttack;
    protected RectF position; //позиція башні
    protected Rect TowerSrc; //анімація башні

    //радіус башні
    protected int radius;
    protected int radius1;
    //кут направлення снаряду
    protected float a;

    protected float recharge;          //час перезарядки башні
    protected float time_recharge = 0; //сумарний час до перезарядки
    protected float time = 0;          //час для анімації магів

    //урон башни
    protected float kick;

    //швидкість польоту снаряду
    protected float speed;

    protected final Matrix matrix = new Matrix ();
    protected final Point point1 = new Point (0, 0);

    protected boolean search = true;  //пошук триває
    protected boolean detected = false; //знайдено моба

    protected boolean boolMagAnimation = false; //якщо треба анімувати мага
    protected int magAnimation = 0;         //кадри анімації

    public Tower (RectF pos, Game game) {
        position = pos;
        this.game = game;
        TowerSrc = new Rect (0, 0, 48, 48); // перший кадр башні, размер 1 картинки мага( анимация мага)
        rectBoom = new RectF ();
        rectAttack = new RectF (pos);
    }

    public void present_T (Canvas canvas) {
        if (search) {
            search (canvas);
        } else if (!detected) {
            detected (canvas);
        } else {
            boom (canvas);
        }
        drawTower(canvas);
    }

    public void update_T () {
        if (search) {
            if (search_Mob (game.getCurrentScreen ().getLevel ().searchMob (position, radius, radius1))) {
                rectCompare (game.getCurrentScreen ().getLevel ().getPosition (point1));
            }
        } else {
            if (detected) {
                boom ();
            } else if (rectCompare (game.getCurrentScreen ().getLevel ().getPosition (point1)))
                game.getCurrentScreen ().getLevel ().strike (point1, kick);
        }
    }

    protected abstract void search (Canvas canvas);

    protected abstract void detected (Canvas canvas);

    protected abstract void boom (Canvas canvas);

    protected abstract void drawTower (Canvas canvas);

    public abstract void towerRestart ();

    public abstract void dispose();

    protected boolean search_Mob (Point point) {

        if (time_recharge < recharge) {
            time_recharge += SurfaceThread.deltaTime;
            return false;
        } else if (point.x != -1) {
            time_recharge = System.nanoTime();
            search = false;
            point1.set(point.x, point.y);
            boolMagAnimation = true;
            return true;
        } else {
            return false;
        }

    }

    protected abstract boolean rectCompare (RectF rectF);

    protected abstract void boom ();

    protected void setAttack () {
        rectAttack.set (position);
    }

}
